import Mock, { Random } from "mockjs"; // 导出随机函数

let comment = {
    id: '@id()',
    author: '@cname()',
    date: "@datetime('yyyy-MM-dd HH:mm')",
    content: '@cparagraph(1, 2)'
}

let commentList = Mock.mock({
  'list|100': [comment]
})

let commentPages = (req) => {
  // req 是一个请求对象，包含: url，type和body属性
  // console.log("分页请求对象：", req);
  let params = JSON.parse(req.body);
  const [currentPage, size, type, total] = [params.currentPage, params.size, params.type, commentList.list.length];

  let start = (currentPage - 1) * size;
  let end = start + size;
  if (end > total) end = total;

  return {
    code: 200,
    message: "OK",
    data: {
      'currentPage': currentPage,
      'size': size,
      'total': total,
      'totalPage': Math.ceil(total / size),
      'list': commentList.list.slice(start, end)
    }
  }
}

let addComment = (req) => {
    let params = JSON.parse(req.body);
    const [content, date, type] = [params.content, params.date, params.type];
    
    let commentTmp = Mock.mock(comment)
    comment.content = content;
    comment.date = date;
    commentList.list.unshift(commentTmp)

    return {
        code: 200,
        message: "OK",
        data: null
    }
}

export default {
    commentPages,
    addComment
}